using System.Data;
using System.Data.SqlClient;
using KZ.Exception;
using KZ.Utilities;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using System;

namespace KZ.Lottery.DAL
{
    public class RuleDetailGroupReportDAL
    {
        private static RuleDetailGroupReport CreateRuleDetailGroupReportFromReader(IDataReader reader)
        {
            RuleDetailGroupReport ruleDetailGroupReport = new RuleDetailGroupReport
            {
                Date = Utility.DateTimeParse(reader[RuleDetailGroupReportColumns.Date.ToString()]),
                Head = Utility.StringParse(reader[RuleDetailGroupReportColumns.Head.ToString()]),
                Tail = Utility.StringParse(reader[RuleDetailGroupReportColumns.Tail.ToString()]),
                RuleDetailID = Utility.StringParse(reader[RuleDetailGroupReportColumns.RuleDetailID.ToString()]),
                GroupID = Utility.StringParse(reader[RuleDetailGroupReportColumns.GroupID.ToString()]),
                ChannelID = Utility.StringParse(reader[RuleDetailGroupReportColumns.ChannelID.ToString()]),
                ChannelName = Utility.StringParse(reader[RuleDetailGroupReportColumns.ChannelName.ToString()]),
                HeadDay = Utility.IntParse(reader[RuleDetailGroupReportColumns.HeadDay.ToString()]),
                HeadCapital = Utility.DecimalParse(reader[RuleDetailGroupReportColumns.HeadCapital.ToString()]),
                HeadActualCapital = Utility.DecimalParse(reader[RuleDetailGroupReportColumns.HeadActualCapital.ToString()]),
                HeadWinAmount = Utility.DecimalParse(reader[RuleDetailGroupReportColumns.HeadWinAmount.ToString()]),
                TailDay = Utility.IntParse(reader[RuleDetailGroupReportColumns.TailDay.ToString()]),
                TailCapital = Utility.DecimalParse(reader[RuleDetailGroupReportColumns.TailCapital.ToString()]),
                TailActualCapital = Utility.DecimalParse(reader[RuleDetailGroupReportColumns.TailActualCapital.ToString()]),
                TailWinAmount = Utility.DecimalParse(reader[RuleDetailGroupReportColumns.TailWinAmount.ToString()])
            };

            return ruleDetailGroupReport;
        }

        public static decimal GetRuleDetailGroupReportForRevenue(DateTime? date)
        {
            try
            {
                DataAccessHelper.SqlCmd.Parameters.Add("Date", SqlDbType.Date).Value = date;
                DataAccessHelper.SqlCmd.Parameters.Add("Revenue", SqlDbType.NVarChar, 50);

                DataAccessHelper.SqlCmd.Parameters["Revenue"].Direction = ParameterDirection.Output;

                DataAccessHelper.ExecuteNonQueryNotClearCommand(CommandType.StoredProcedure, KZ_StoreName.RuleDetailGroupReportGetForRevenue);

                decimal revenue = Utility.DecimalParse(DataAccessHelper.SqlCmd.Parameters["Revenue"].Value);

                DataAccessHelper.ClearCommand();

                return revenue;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetRuleDetailGroupReportForRevenue", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEREPORT_GET);
            }
        }

        public static void GetFirstDateLastDate(out DateTime? firstDate, out DateTime? lastDate)
        {
            try
            {
                firstDate = null;
                lastDate = null;

                DataAccessHelper.SqlCmd.Parameters.Add("FirstDate", SqlDbType.Date).Direction = ParameterDirection.Output;
                DataAccessHelper.SqlCmd.Parameters.Add("LastDate", SqlDbType.Date).Direction = ParameterDirection.Output;

                DataAccessHelper.ExecuteNonQueryNotClearCommand(CommandType.StoredProcedure, KZ_StoreName.RuleDetailGroupReportGetFirstDateLastDate);

                firstDate = Utility.DateTimeNParse(DataAccessHelper.SqlCmd.Parameters["FirstDate"].Value);
                lastDate = Utility.DateTimeNParse(DataAccessHelper.SqlCmd.Parameters["LastDate"].Value);

                DataAccessHelper.ClearCommand();
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetFirstDateLastDate", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEREPORT_GET);
            }
        }

        public static DataTable GetRuleDetailGroupReportListForRuleChannel(string ruleID, DateTime from, DateTime to, string tableName)
        {
            try
            {
                DataTable dt = new DataTable();

                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleID;
                DataAccessHelper.SqlCmd.Parameters.Add("From", SqlDbType.Date).Value = from;
                DataAccessHelper.SqlCmd.Parameters.Add("To", SqlDbType.Date).Value = to;
                DataAccessHelper.SqlCmd.Parameters.Add("TableName", SqlDbType.VarChar).Value = tableName;

                dt.Load(DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.RuleDetailGroupReportGetListForRuleChannel));

                return dt;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetRuleDetailGroupReportListForRuleChannel", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEREPORT_GET);
            }
        }

        public static DataTable GetRuleDetailGroupReportListForChannel(string ruleID, DateTime from, DateTime to, string tableName)
        {
            try
            {
                DataTable dt = new DataTable();

                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleID;
                DataAccessHelper.SqlCmd.Parameters.Add("From", SqlDbType.Date).Value = from;
                DataAccessHelper.SqlCmd.Parameters.Add("To", SqlDbType.Date).Value = to;
                DataAccessHelper.SqlCmd.Parameters.Add("TableName", SqlDbType.VarChar).Value = tableName;

                dt.Load(DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.RuleDetailGroupReportGetListForChannel));

                return dt;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetRuleDetailGroupReportListForChannel", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEREPORT_GET);
            }
        }

        public static DataTable GetRuleDetailGroupReportListForChannelTotal(string ruleID, DateTime from, DateTime to, string tableName)
        {
            try
            {
                DataTable dt = new DataTable();

                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleID;
                DataAccessHelper.SqlCmd.Parameters.Add("From", SqlDbType.Date).Value = from;
                DataAccessHelper.SqlCmd.Parameters.Add("To", SqlDbType.Date).Value = to;
                DataAccessHelper.SqlCmd.Parameters.Add("TableName", SqlDbType.VarChar).Value = tableName;

                dt.Load(DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.RuleDetailGroupReportGetListForChannelTotal));

                return dt;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetRuleDetailGroupReportListForChannel", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEREPORT_GET);
            }
        }

        public static DataTable GetRuleDetailGroupReportListForDate(string ruleID, DateTime from, DateTime to, string tableName)
        {
            try
            {
                DataTable dt = new DataTable();

                DataAccessHelper.SqlCmd.Parameters.Add("RuleID", SqlDbType.NVarChar).Value = ruleID;
                DataAccessHelper.SqlCmd.Parameters.Add("From", SqlDbType.Date).Value = from;
                DataAccessHelper.SqlCmd.Parameters.Add("To", SqlDbType.Date).Value = to;
                DataAccessHelper.SqlCmd.Parameters.Add("TableName", SqlDbType.VarChar).Value = tableName;

                dt.Load(DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.RuleDetailGroupReportGetListForDate));

                return dt;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetRuleDetailGroupReportListForDate", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEREPORT_GET);
            }
        }

        public bool InsertRuleDetailGroupReport(RuleDetailGroupReport ruleDetailGroupReport)
        {
            try
            {
                DataAccessHelper.SqlCmd.Parameters.Add("Date", SqlDbType.Date).Value = ruleDetailGroupReport.Date;
                DataAccessHelper.SqlCmd.Parameters.Add("Head", SqlDbType.VarChar).Value = ruleDetailGroupReport.Head;
                DataAccessHelper.SqlCmd.Parameters.Add("Tail", SqlDbType.VarChar).Value = ruleDetailGroupReport.Tail;
                DataAccessHelper.SqlCmd.Parameters.Add("RuleDetailID", SqlDbType.NVarChar).Value = ruleDetailGroupReport.RuleDetailID;
                DataAccessHelper.SqlCmd.Parameters.Add("GroupID", SqlDbType.NVarChar).Value = ruleDetailGroupReport.GroupID;
                DataAccessHelper.SqlCmd.Parameters.Add("ChannelID", SqlDbType.NVarChar).Value = ruleDetailGroupReport.ChannelID;
                DataAccessHelper.SqlCmd.Parameters.Add("HeadDay", SqlDbType.Int).Value = ruleDetailGroupReport.HeadDay;
                DataAccessHelper.SqlCmd.Parameters.Add("HeadCapital", SqlDbType.Decimal).Value = ruleDetailGroupReport.HeadCapital;
                DataAccessHelper.SqlCmd.Parameters.Add("HeadActualCapital", SqlDbType.Decimal).Value = ruleDetailGroupReport.HeadActualCapital;
                DataAccessHelper.SqlCmd.Parameters.Add("HeadWinAmount", SqlDbType.Decimal).Value = ruleDetailGroupReport.HeadWinAmount;
                DataAccessHelper.SqlCmd.Parameters.Add("TailDay", SqlDbType.Int).Value = ruleDetailGroupReport.TailDay;
                DataAccessHelper.SqlCmd.Parameters.Add("TailCapital", SqlDbType.Decimal).Value = ruleDetailGroupReport.TailCapital;
                DataAccessHelper.SqlCmd.Parameters.Add("TailActualCapital", SqlDbType.Decimal).Value = ruleDetailGroupReport.TailActualCapital;
                DataAccessHelper.SqlCmd.Parameters.Add("TailWinAmount", SqlDbType.Decimal).Value = ruleDetailGroupReport.TailWinAmount;
                DataAccessHelper.ExecuteNonQueryWithTransaction(CommandType.StoredProcedure, KZ_StoreName.RuleDetailGroupReportInsert);
                return true;
            }
            catch (SqlException ex)
            {
                Log.LogEx("InsertRuleDetailGroupReport", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEDETAILGROUPREPORT_INSERT);
            }
        }

        public bool UpdateRuleDetailGroupReportDay()
        {
            try
            {
                DataAccessHelper.ExecuteNonQueryWithTransaction(CommandType.StoredProcedure, KZ_StoreName.RuleDetailGroupReportUpdateDay);
                return true;
            }
            catch (SqlException ex)
            {
                Log.LogEx("UpdateRuleDetailGroupReportDay", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_RULEDETAILGROUPREPORT_UPDATE);
            }
        }
    }
}
